package link.ten_fei.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import link.ten_fei.Bean.Pl_List;
import link.ten_fei.util.ConnectionFactory;

public class QueryMRDaoImpl implements QueryMRDao {

	public List<Pl_List> find(String pl_name, String flag, int id, int doc_id, int doc_judge) {
		// 1.获取数据库连接
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement preparedStatement = null;
		ResultSet rs = null;
		if (doc_judge == 0) {
			String name = null;
			if (flag != null && flag.equals("病人名字")) {
				name = "pl_name";
			} else if (flag != null && flag.equals("医生名字")) {
				name = "pl_doctor";
			}
			try {
				// 2.准备sql语句
				String sql = null;
				if ((pl_name == null || pl_name.equals("")) && id == 0) {
					sql = "select * from pl_list limit 500";
				} else if (pl_name != null && !pl_name.equals("") && id == 0) {
					sql = "select * from pl_list where " + name + "= '" + pl_name + "'";
				} else if ((pl_name == null || pl_name.equals("")) && id != 0) {
					sql = "select * from pl_list where id=" + id;
				}
				// 3.获取连接通路
				preparedStatement = connection.prepareStatement(sql);
				// 4.执行sql语句,返回查询的结果,封装在rs中
				rs = preparedStatement.executeQuery();
				// 5.将查询到的结果对象封装到一个集合中
				List<Pl_List> lists = new ArrayList<Pl_List>();
				// 查询的结果包含表头信息
				while (rs.next()) {
					Pl_List list = new Pl_List();
					// 每一行的数据封装在一个实体bean中，根据字段获取字段值，该字段是什么类型就get什么类型
					list.setId(rs.getInt("id"));
					list.setPl_number(rs.getString("pl_number"));
					list.setPl_date(rs.getString("pl_date"));
					list.setPl_name(rs.getString("pl_name"));
					list.setPl_sex(rs.getString("pl_sex"));
					list.setPl_age(rs.getString("pl_age"));
					list.setPl_expensetype(rs.getString("pl_expensetype"));
					list.setPl_diagnosis(rs.getString("pl_diagnosis"));
					list.setPl_department(rs.getString("pl_department"));
					list.setPl_rp(rs.getString("pl_rp"));
					list.setPl_doctor(rs.getString("pl_doctor"));
					list.setPl_deployment(rs.getString("pl_deployment"));
					list.setPl_review(rs.getString("pl_review"));
					list.setPl_amount(rs.getString("pl_amount"));
					lists.add(list);
				}
				return lists;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				ConnectionFactory.close(connection, preparedStatement, rs);
			}
		} else {
			try {
				String sql = null;
				if ((pl_name == null || pl_name.equals("")) && id == 0) {
					sql = "select * from pl_list where pl_doctor=(select doc_name from doc_user where id=" + doc_id
							+ " ) limit 300";
				} else if (pl_name != null && !pl_name.equals("") && id == 0) {
					sql = "select * from pl_list where pl_doctor=(select doc_name from doc_user where id=" + doc_id
							+ ") and pl_name= '" + pl_name + "'";
				} else if ((pl_name == null || pl_name.equals("")) && id != 0) {
					sql = "select * from pl_list where id=" + id;
				}
				preparedStatement = connection.prepareStatement(sql);
				rs = preparedStatement.executeQuery();
				List<Pl_List> lists = new ArrayList<Pl_List>();
				// 查询的结果包含表头信息
				while (rs.next()) {
					Pl_List list = new Pl_List();
					// 每一行的数据封装在一个实体bean中，根据字段获取字段值，该字段是什么类型就get什么类型
					list.setId(rs.getInt("id"));
					list.setPl_number(rs.getString("pl_number"));
					list.setPl_date(rs.getString("pl_date"));
					list.setPl_name(rs.getString("pl_name"));
					list.setPl_sex(rs.getString("pl_sex"));
					list.setPl_age(rs.getString("pl_age"));
					list.setPl_expensetype(rs.getString("pl_expensetype"));
					list.setPl_diagnosis(rs.getString("pl_diagnosis"));
					list.setPl_department(rs.getString("pl_department"));
					list.setPl_rp(rs.getString("pl_rp"));
					list.setPl_doctor(rs.getString("pl_doctor"));
					list.setPl_deployment(rs.getString("pl_deployment"));
					list.setPl_review(rs.getString("pl_review"));
					list.setPl_amount(rs.getString("pl_amount"));
					lists.add(list);
				}
				return lists;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				ConnectionFactory.close(connection, preparedStatement, null);
			}
		}

		return null;
	}

	@Override
	public int deleteById(int id) {
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement preparedStatement = null;
		int num = 0;
		try {
			String sql = "delete from pl_list where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			num = preparedStatement.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(connection, preparedStatement, null);
		}
		return num;
	}

}
